<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>站内信列表</title>
    <link rel="stylesheet" type="text/css" th:href="@{/manager/easyui/themes/default/easyui.css}">
    <link rel="stylesheet" type="text/css" th:href="@{/manager/easyui/themes/icon.css}">
    <script type="text/javascript" th:src="@{/manager/easyui/jquery.min.js}"></script>
    <script type="text/javascript" th:src="@{/manager/easyui/jquery.easyui.min.js}"></script>
	<script type="text/javascript">
	   $(function(){
	       $('#noticelist').datagrid({  
      				title:'站内信列表',  
      				iconCls:'icon-save',
                    url:"/manager/notice/findAll",
      				columns:[
                          [
                					       {field:'title',title:'标题',width:200,align:'center'},  
                					       {field:'info',title:'内容',width:300,align:'center'},  
                					       {field:'managerusername',title:'发布人员',width:100,align:'center'},
                                 {field:'username',title:'接收用户',width:100,align:'center',formatter:function (val,row){
                                         return val === null?"-":val;
                                     }},
                                 {field:'createdate',title:'发布日期',width:100,align:'center',formatter:function (val,row){
                                         var createdate = new Date(val);
                                         var year = createdate.getFullYear();
                                         var month = ('' + (createdate.getMonth() + 1)).slice(-2);
                                         var day = ('' + createdate.getDate()).slice(-2);
                                         return year + '-' + month + '-' + day;
                                     }}
                          ]
                    ],
      				toolbar: "#tooldiv",  
      				pagination: true,
      				singleSelect:true
			});


          
          //初始化修改窗口  
         $('#dg').dialog({
  			    title: '新建通知',
  			    width: 500,
  			    height: 400,
  			    closed: true,
  			    cache: false,
  			    modal: true,
  			    iconCls: 'icon-save',
            buttons: [{
                    text:'提交',
                    iconCls:'icon-ok',
                    handler:function(){
                        if ($('#dg .easyui-textbox').val().trim() === "" || $('#dg textarea').val().trim() === "") {
                            alert("标题和信息内容都不能为空或仅包含空格!");
                        }else {
                            $.ajax({
                                dataType: "json",
                                url: "/manager/notice/add",
                                data: {
                                    "title": $('#dg .easyui-textbox').val(),
                                    "info": $('#dg textarea').val(),
                                    "manageruserid": sessionStorage.getItem("USER_ID")
                                },
                                success: function (result) {
                                    if (result.status == 200) {
                                        alert(result.msg);
                                        $('#noticelist').datagrid('reload');
                                        $('#dg').dialog('close');
                                    }
                                },
                                error: function (xhr, status, error) {
                                    alert("请求失败: " + xhr.responseText); // 处理 AJAX 请求错误
                                }
                            });
                        }
                    }
                },{
                    text:'关闭',
                    handler:function(){
                        $('#dg').dialog('close');
                    }
                }]
            });

  	});

       //打开新建通知界面
       function opendg(){
          $('#dg').dialog('open');
          $('#dg .easyui-textbox').textbox('setValue', '');
          $('#dg textarea').val("");
       }

       function deleteById(){
           var selectedRow = $("#noticelist").datagrid('getSelected');
           if(selectedRow){
                   $.ajax({
                       dataType: "json",
                       url: "/manager/notice/delete",
                       data: {"id":selectedRow.id},
                       success: function (result) {
                            if(result.status == 200) {
                                alert(result.msg);
                                $('#noticelist').datagrid('reload');
                            }
                       },
                       error: function (xhr, status, error) {
                           alert("请求失败: " + xhr.responseText); // 处理 AJAX 请求错误
                       }
                   });
           }else {
               alert("请选中列表对应的通知信息!")
           }
       }
	</script>
</head>
<body>
  <div id="tooldiv">
      <a  class="easyui-linkbutton" data-options="iconCls:'icon-remove'" href="javascript:deleteById();">删除</a>
      <a  class="easyui-linkbutton" data-options="iconCls:'icon-add'" onclick="opendg();">新建</a>
  </div>
  <table id="noticelist"></table>
    <div id="dg">
           <div align="center" style="margin-top: 30px">
            <div style="margin-bottom:20px">
                <input class="easyui-textbox"  style="width:50%" data-options="label:'标题:'"  />
            </div>
            <div style="margin-bottom:20px">
                <textarea cols="50" rows="10"></textarea>
            </div>
        </div>
   </div>
</body>
</html>